<template>
  <div class="app-container">
    <!-- 1. 优化的搜索表单 -->
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户名" prop="username">
        <el-input
          v-model="queryParams.username"
          placeholder="请输入用户名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="打卡地点" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入打卡地点"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="打卡时间">
        <el-date-picker
          v-model="dateRange"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 2. 操作按钮行（已简化） -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['iot:data:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['iot:data:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 3. 优化的数据表格 -->
    <el-table v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="记录ID" align="center" prop="id" />
      <el-table-column label="打卡图片" align="center" prop="image">
        <template slot-scope="scope">
          <image-preview :src="scope.row.image" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="用户名" align="center" prop="username" />
      <el-table-column label="打卡设备" align="center" prop="deviceName" />
      <el-table-column label="打卡地点" align="center" prop="address" :show-overflow-tooltip="true" />
      <el-table-column label="打卡时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
           <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['iot:data:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 4. 详情抽屉 -->
    <el-drawer :title="title" :visible.sync="open" direction="rtl" size="500px">
        <div class="drawer-content">
            <el-descriptions class="margin-top" :column="1" border>
                <el-descriptions-item label="记录ID">{{ form.id }}</el-descriptions-item>
                <el-descriptions-item label="用户名">{{ form.username }}</el-descriptions-item>
                <el-descriptions-item label="打卡图片">
                    <image-preview :src="form.image" :width="200" :height="200"/>
                </el-descriptions-item>
                <el-descriptions-item label="打卡设备">{{ form.deviceName }}</el-descriptions-item>
                <el-descriptions-item label="打卡地点">{{ form.address }}</el-descriptions-item>
                <el-descriptions-item label="打卡时间">{{ parseTime(form.createTime) }}</el-descriptions-item>
            </el-descriptions>
        </div>
    </el-drawer>
  </div>
</template>

<script>
import { listData, getData, delData } from "@/api/iot/checkData";

import ImagePreview from '@/components/ImagePreview';

export default {
  name: "CheckData",
  components: {
    ImagePreview
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      total: 0,
      dataList: [],
      title: "",
      open: false, // 控制抽屉的显示
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        username: null,
        address: null,
        // deviceId: null, // 可以保留，如果需要按设备ID精确搜索
      },
      // 表单参数（用于详情显示）
      form: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询打卡记录列表 */
    getList() {
      this.loading = true;
      // 将日期范围添加到查询参数中
      this.addDateRange(this.queryParams, this.dateRange);
      listData(this.queryParams).then(response => {

        this.dataList = response.rows.map(item => {
            if (item.createTime) item.createTime = item.createTime * 1000;
            return item;
        });
        this.total = response.total;
        this.loading = false;
      });
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        username: null,
        image: null,
        address: null,
        createTime: null,
        deviceId: null,
        deviceName: null, // 增加设备名称字段
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 详情按钮操作 */
    handleView(row) {
      this.reset();
      const id = row.id || this.ids[0];
      getData(id).then(response => {
        // 假设详情接口也返回了 deviceName
        this.form = response.data;
        if (this.form.createTime) this.form.createTime = this.form.createTime * 1000;
        this.open = true;
        this.title = "打卡记录详情";
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除打卡记录编号为"' + ids + '"的数据项？').then(function() {
        return delData(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('iot/data/export', {
        ...this.queryParams
      }, `check_data_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style scoped>
.drawer-content {
    padding: 20px;
}
</style>
